package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConvert;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.application.dto.SubjectMappingDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.entity.SubjectMappingBO;
import com.jingdianjichi.subject.domain.service.Imp.SubjectLabelDomainServiceImp;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectLabelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签控制器
 */
@RestController
@Slf4j
@RequestMapping("/subject/label")
public class SubjectLabelController {
    @Resource
    private SubjectLabelDomainServiceImp subjectLabelDomainServiceImp;
    @Autowired
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add (@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("SubjectCategoryDTO:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签的名称不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertLabelDTOToBo(subjectLabelDTO);
            subjectLabelBO.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
            Boolean result=subjectLabelDomainServiceImp.add(subjectLabelBO);
            return Result.ok(result);
        }catch (Exception e) {
                log.error("error: {}", e.getMessage());
                return Result.fail("新增标签失败！！");
        }

    }
    @PostMapping("/update")
    public Result<Boolean> update (@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("SubjectCategoryDTO:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名称不可为空！");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertLabelDTOToBo(subjectLabelDTO);
            Boolean result =subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("error: {}", e.getMessage());
            return Result.fail("修改失败！");
        }
    }
    @PostMapping("/delete")
    public Result<Boolean> delete (@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("SubjectCategoryDTO:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"id不可为空！");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertLabelDTOToBo(subjectLabelDTO);
            Boolean result =subjectLabelDomainService.delete(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("error: {}", e.getMessage());
            return Result.fail("删除失败！");
        }
    }
    /**
     * 查询分类下标签
     */
    @PostMapping("/queryByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}",
                        JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConvert.INSTANCE.convertLabelDTOToBo(subjectLabelDTO);
            List<SubjectLabelBO> resultList = subjectLabelDomainService.queryByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConvert.INSTANCE.convertBoToCategoryDTOList(resultList);
            return Result.ok(subjectLabelDTOS);
        } catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId.error:{}", e.getMessage(), e);
            return Result.fail("查询分类下标签失败");
        }
    }

}

